import QtQuick 1.0

Rectangle {
    id: dialog

    property alias title : titleText.text
    property alias information : informativeText.text
    property string iconFilename
    property bool modal: false
    property int type: 0
    property string backgroundColor: root.constants.defaultBackgroundColor
    property string backgroundColorAccent: root.constants.highlightColor
    property string foregroundColor: root.constants.highlightColorAccent

    signal closed

    /*!
      Displays the dialog.
    */
    function doDisplay()
    {
        dialog.opacity = 1.0;
        root.blur.state = "on";

        if (!dialog.modal) {
            // The dialog is not modal, start the timer to close the dialog.
            if (closeTimer.running) {
                closeTimer.stop();
            }

            closeTimer.start();
        }

        if (dialog.type == 1) {
            // Error dialog.
            backgroundColorAccent = "darkred";
            foregroundColor = "red";
        }
    }

    /*!
      Closes the dialog and emits closed signal.
    */
    function doClose()
    {
        root.blur.state = "off";
        dialog.opacity = 0;
        dialog.closed();
    }

    radius: width * root.constants.defaultRelativeRadius / 2
    border.width: root.constants.defaultBorderWidth
    border.color: backgroundColorAccent
    smooth: true

    gradient: Gradient {
        GradientStop { position: 0.0; color: dialog.backgroundColorAccent }
        GradientStop {
            position: root.constants.dialogTitleHeight / dialog.height
            color: foregroundColor
        }
        GradientStop {
            position: root.constants.dialogTitleHeight / dialog.height + 0.01
            color: dialog.backgroundColor
        }
        GradientStop { position: 0.7; color: dialog.backgroundColor }
        GradientStop { position: 1.0; color: dialog.backgroundColorAccent }
    }

    Behavior on opacity {
        NumberAnimation { duration: root.constants.shortAnimationDuration }
    }

    Column {
        anchors.fill: parent

        Text {
            id: titleText
            width: parent.width
            height: root.constants.dialogTitleHeight
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            color: root.constants.defaultBackgroundColor
            opacity: text.length == 0 ? 0 : 1
            font.pixelSize: root.constants.defaultFontPixelSize
            font.bold: true
            wrapMode: Text.Wrap
        }

        Rectangle {
            id: informativeTextRectangle
            width: parent.width
            height: parent.height - titleText.height
            color: root.constants.transparentColor

            Row {
                id: dialogContent
                anchors.fill: parent
                anchors.margins: {
                    top: root.constants.defaultMargin
                    right: root.constants.defaultMargin
                    bottom: root.constants.defaultMargin
                    left: root.constants.defaultMargin
                }
                spacing: root.constants.defaultLayoutSpacing

                Image {
                    id: dialogIcon
                    source: dialog.iconFilename
                }

                Text {
                    id: informativeText
                    width: parent.width - dialogIcon.width - dialogContent.spacing
                    color: root.constants.defaultForegroundColor
                    font.pixelSize: root.constants.defaultFontPixelSize
                    wrapMode: Text.WordWrap
                }
            }
        }
    }

    MouseArea {
        anchors.fill: parent

        onClicked: {
            haptics.buttonTapFeedback();
            dialog.doClose();
        }
    }

    Timer {
        id: closeTimer
        interval: root.constants.dialogTimeout
        running: false
        repeat: false

        onTriggered: {
            console.debug("dialog.closeTimer: onTriggered()");
            dialog.doClose();
        }
    }
}

// End of file.
